<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .top_button_con {
    width: 100%;
    display: flex;
    .top_item_button {
      width: 80px;
      height: 35px;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid #ddd;
      font-size: 13px;
      cursor: pointer;
    }
    .select_button {
      background: #1890ff;
      color: #fff;
    }
  }
  .main_con {
    width: 100%;
    height: calc(100% - 35px);
    box-sizing: border-box;
    .form_con {
      width: 100%;
      height: 100%;
      .form_container {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        height: 100%;
      }
      .equ_item {
        height: 40px;
        line-height: 40px;
        font-size: 13px;
        padding: 0 5px;
        box-sizing: border-box;
        width: 100%;
        border-bottom: 1px solid #ddd;
      }
      .bg_con {
        background: #ddd;
      }
    }
  }
  ::v-deep {
    .el-form-item__label {
      font-size: 12px;
    }
    .el-form-item {
      // margin-bottom: 3px;
    }
  }
}
.button_con {
  width: 100%;
  padding: 10px;
  display: flex;
  justify-content: center;
}
</style>
<template>
  <div class="container">
    <div v-if="indexButton === 2" class="form_con">
      <el-form
        ref="formdata"
        :rules="rules"
        :model="formdata"
        class="form_container"
        label-width="110px"
        label-position="right"
      >
        <el-form-item label="测试量程(米)">
          <el-select
            v-model="formdata.range_dist"
            placeholder="请选择"
            style="width: 100%"
            @change="equiObj_fun"
          >
            <el-option
              v-for="(item, index) in equiData.testpara_table"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="测试脉宽(ns)">
          <el-select
            v-model="formdata.plus_width"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in equiData.pluswidth_table"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="测试时间(s)">
          <el-select
            v-model="formdata.test_time"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option :value="10" label="10"></el-option>
            <el-option :value="15" label="15"></el-option>
            <el-option :value="20" label="20"></el-option>
            <el-option :value="30" label="30"></el-option>
            <el-option :value="60" label="60"></el-option>
            <el-option :value="120" label="120"></el-option>
            <el-option :value="180" label="180"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="测试波长(nm)">
          <el-select
            v-model="formdata.wave_length"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in equiData.wave_lengthArr"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="折射率(dB)">
          <el-input
            v-model="formdata.IOR"
            placeholder="折射率"
          ></el-input>
        </el-form-item>
        <el-form-item label="回散系数(dB)">
          <el-input
            v-model="formdata.back_scatter"
            placeholder="回散系数"
          ></el-input>
        </el-form-item>
        <el-form-item label="结束门限(dB)">
          <el-input
            v-model="formdata.final_threashold"
            placeholder="结束门限"
          ></el-input>
        </el-form-item>
        <el-form-item label="反射门限(dB)">
          <el-input
            v-model="formdata.reflect_threashold"
            placeholder="反射门限"
          ></el-input>
        </el-form-item>
        <el-form-item label="非反射门限(dB)">
          <el-input
            v-model="formdata.non_reflect_threashold"
            placeholder="非反射门限"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="button_con">
        <el-button type="primary" @click="startFun">启动测试</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserTestParam } from "@/api/base/userTestParam";
export default {
  name: "equiParma",
  props: {
    equiData: {
      type: Object,
      default: () => {
        return {};
      },
    },
    channel: {
      type: Number,
      default: 999,
    },
    deviceNo: {
      type: String,
      default: "",
    },
    testParamVis: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    equiData: {
      handler(val) {
        // console.log("vallll", val);
      },
      deep: true,
      immediate: true, // 第一次监听时立即触发
    },
    testParamVis: {
      handler(val) {
        if (val) {
          this.getLastTestParam();
        }
      },
      immediate: true, // 第一次监听时立即触发
    },
  },
  data() {
    return {
      formdata: {
        back_scatter: 52.5,
        final_threashold: 3,
        non_reflect_threashold: 0.2,
        plus_width: "自动",
        range_dist: "自动",
        reflect_threashold: 65,
        test_mode: 1,
        test_time: 15,
        wave_length: 1550,
        IOR: 1.467,
      },
      rules: {},
      indexButton: 2,
      equArr: [1, 2, 3, 4, 5],
    };
  },
  methods: {
    equiObj_fun(e) {
      if (e === "自动") {
        this.formdata.plus_width = "自动";
      }
    },
    startFun() {
      //开始测试
      this.$emit("startFun", this.formdata);
    },
    getLastTestParam() {
      //获取测试参数
      let that = this;
      getUserTestParam(this.deviceNo, this.channel).then((res) => {
        if(!res.data) res.data = {};
        this.formdata.back_scatter = res.data.paramBackScatter
          ? that.fixFun(res.data.paramBackScatter)
          : 52.5;
        this.formdata.final_threashold = res.data.paramFinalThreshold
          ? that.fixFun(res.data.paramFinalThreshold)
          : 3;
        this.formdata.non_reflect_threashold = res.data.paramNonReflectThreshold
          ? that.fixFun(res.data.paramNonReflectThreshold)
          : 0.2;
        this.formdata.plus_width = res.data.paramPw
          ? that.fixFun(res.data.paramPw)
          : "自动";
        this.formdata.range_dist = res.data.paramRd
          ? that.fixFun(res.data.paramRd)
          : "自动";
        this.formdata.reflect_threashold = res.data.paramReflectThreshold
          ? that.fixFun(res.data.paramReflectThreshold)
          : 65;
        this.formdata.test_mode = res.data.paramTestMode
          ? that.fixFun(res.data.paramTestMode)
          : 1;
        this.formdata.test_time = res.data.paramTestTime
          ? that.fixFun(res.data.paramTestTime)
          : 15;
        this.formdata.wave_length = res.data.paramWaveLength
          ? that.fixFun(res.data.paramWaveLength)
          : 1550;
        this.formdata.IOR = res.data.paramIor
          ? that.fixFun(res.data.paramIor)
          : 1.467;
      });
    },
    fixFun(e, item) {
      if (!e && e != 0) {
        return "-";
      } else if (e == 0) {
        return e;
      } else if (e == "-") {
        return "-";
      } else if (e == "null") {
        return "-";
      } else if (e == "-null") {
        return "-";
      } else {
        let num = parseFloat(e);
        let strData = num.toString();
        let splitData = strData.split(".");
        if (num % 1 === 0) {
          return num;
        } else if (splitData.length > 0) {
          if (splitData[1].length >= 4) {
            return parseFloat(num.toFixed(4));
          } else if (splitData[1].length === 3) {
            return parseFloat(num.toFixed(3));
          } else if (splitData[1].length === 2) {
            return parseFloat(num.toFixed(2));
          } else if (splitData[1].length === 1) {
            return parseFloat(num.toFixed(1));
          } else {
            return parseFloat(num);
          }
        } else {
          return num;
        }
      }
    },
  },
};
</script>
